<?php
require_once(dirname(__FILE__) . '/PortalUtil.class.php');

class Data extends PortalUtil {
	public $tbl = Constant::tbl_wifi_data;

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	function _where($request) {
		$uid_s = $request['uid_s'];
	
		$where = " WHERE 1=1";
	
		if (!empty($uid_s)) {
			$where .= " AND uid = '" . $uid_s . "'";
		}
	
		// $where .= $this->_where_default($request);
		// $where = $this->_where_aa($where);
	
		return $where;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function layout($controller, $request) {
		$this->pretreat($controller);
		
		if (empty($_SESSION['username'])) {
			$controller->display($request, 'wlan/login');
			return true;
		}
		
		$request['uid_s'] = $_SESSION['uid'];
		
		$this->nav['home'] = true;
		
		$this->findPara($controller, $request);
		
		// $this->orderby_s = "id";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$pager_option = array (
			'db' => $db,
			'sql' => $sql,
			'PageSize' => 8,
			'CurrentPageID' => $request['p'],
			'numItems' => $request['n']
		);
		
		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();
		
		if ($pager->number > 0) {
		
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
		
				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
		
		$controller->display($request, 'data/index');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		
		if ($row['incoming'] > 0) {
			$row['amount_s'] = sprintf("+%s", $row['incoming']);
		} else {
			$row['amount_s'] = sprintf("-%s", $row['outgoing']);
		}
	
		return $row;
	}
	
	function transfer($controller, $request) {
		$this->pretreat($controller);
	
		if (empty($_SESSION['uid'])) {
			$controller->display($request, 'wlan/login');
			return true;
		}
		
		$uid = $_SESSION['uid'];
		
		require_once(dirname(__FILE__) . '/User.class.php');
		$aa = new User();
		
		$user = $aa->_user($uid);
		
		$controller->assign_by_ref('user', $user);
		
		$controller->display($request, 'data/transfer');
		return true;
	}
	
	function transferPost($controller, $request) {
		$this->pretreat($controller);
	
		if (empty($_SESSION['uid'])) {
			$controller->display($request, 'wlan/login');
			return true;
		}
		
		$my = $_SESSION['uid'];
		
		require_once(dirname(__FILE__) . '/User.class.php');
		$aa = new User();
		
		$user = $aa->_user($my);
		
		$name = $request['name'];
		$uid = $request['uid'];
		$amount = $request['amount'];
		
		$content = $request['content'];
		
		$ret = array('status'=>1);
		
		if (empty($name) || empty($uid) || empty($amount)) {
			$ret['status'] = 9;
			echo json_encode($ret);
			return false;
		}
		
		if ($uid == $my) {
			$ret['status'] = 8;
			echo json_encode($ret);
			return false;
		}
		
		if (!preg_match("/^(\d+)$/i", $amount)) {
			$ret['status'] = 2;
			echo json_encode($ret);
			return false;
		}
		
		if ($row = $aa->_user($uid)) {
			if ($row['name'] <> $name) {
				$ret['status'] = 3;
				echo json_encode($ret);
				return false;
			}
			
		} else {
			$ret['status'] = 4;
			echo json_encode($ret);
			return false;
		}
		
		if ($amount > $user['r1'] || $amount > $user['r2']) {
			$ret['status'] = 5;
			echo json_encode($ret);
			return false;
		}
		
		$content1 = sprintf("%s,%s", $name, $content);
		$content2 = sprintf("%s,%s", $user['name'], $content);
		
		$this->_transfer($my, $uid, 0, $amount, $content1);
		$this->_transfer($uid, $my, $amount, 0, $content2);
		
		echo json_encode($ret);
		return true;
	}
	
	function _transfer($uid, $opposite, $incoming, $outgoing, $content) {
		
		require_once(dirname(__FILE__) . '/User.class.php');
		$aa = new User();
		
		$balance = $aa->_transfer($uid, $incoming, $outgoing);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(uid, opposite, incoming, outgoing, balance, content, createtime) 
				VALUES('%s', '%s', '%s', '%s', '%s', '%s', NOW())";
		$sql = sprintf($fmt, $this->tbl, $uid, $opposite, $incoming, $outgoing, $balance, $content);
		debug($sql);
		$db->query($sql);
	}
}
?>
